#include <d3d9.h>
#include <d3dx9.h>
#include "Direct3d.h"
#include "sprite.h"
#include "cDayState.h"

extern int xRes,yRes;

/************************************************************************/
/* cMorning                                                             */
/************************************************************************/
cMorning::cMorning(char *backGroundPath,char *creaturePath)
{

	creature=new cSprite();
	if(!creature)
		exit(1);
	creature->LoadFromFile(creaturePath);
	

	backGround=new cSprite();
	if(!backGround)
		exit(1);
	backGround->LoadFromFile(backGroundPath);
	

	animation=NULL;
	population=0;
	

}

void cMorning::Draw(int new_population)
{

	new_population=min(new_population,7);

	backGround->Draw();


	if(new_population>population)
	{
		animation=(Point*)realloc(animation,sizeof(Point)*new_population);
		for(int i=population;i<new_population;i++)
			animation[i]=Point(0,(i+1)*90);
		

		population=new_population;
	}
	else if(new_population<population)
	{
		animation=(Point*)realloc(animation,sizeof(Point)*new_population);
		population=new_population;
	}

	for(int j=0;j<population;j++)
	{
		animation[j].x+=0.7f;
		if(animation[j].x>xRes)
			animation[j].x=0;
		animation[j].y=50*sin(animation[j].x/20)+90*(j+1);
		creature->DrawAt(animation[j].x,animation[j].y);
	}
	
}

void cMorning::reset()
{
	if(animation)
	{
		for(int i=0;i<population;i++)
			animation[i]=Point(0,(i+1)*90);
	}
}





/************************************************************************/
/* cNight                                                               */
/************************************************************************/
cNight::cNight(char *backGroundPath,char *creaturePath)
{

	creature=new cSprite();
	if(!creature)
		exit(1);
	creature->LoadFromFile(creaturePath);
	

	backGround=new cSprite();
	if(!backGround)
		exit(1);
	backGround->LoadFromFile(backGroundPath);
	

	animation=NULL;
	population=0;
	

}

void cNight::Draw(int new_population)
{

	new_population=min(new_population,7);

	backGround->Draw();


	if(new_population>population)
	{
		animation=(Point*)realloc(animation,sizeof(Point)*new_population);
		for(int i=population;i<new_population;i++)
			animation[i]=Point(0,(i+1)*90);
		

		population=new_population;
	}
	else if(new_population<population)
	{
		animation=(Point*)realloc(animation,sizeof(Point)*new_population);
		population=new_population;
	}

	for(int j=0;j<population;j++)
	{
		animation[j].x-=1;
		if(animation[j].x<0)
			animation[j].x=xRes;
		animation[j].y=50*sin(animation[j].x/20)+90*(j+1);
		creature->DrawAt(animation[j].x,animation[j].y);
	}
	
}



void cNight::reset()
{
	if(animation)
	{
		for(int i=0;i<population;i++)
			animation[i]=Point(xRes,(i+1)*90);
	}
}

